﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reddit.EndpointRetriever
{
    public class Endpoint
    {
        protected IList<string> endpointPieces = new List<string>();
        protected IList<EndpointOption> endpointOptions = new List<EndpointOption>();

        public IEnumerable<string> Pieces
        {
            get { return endpointPieces; }
        }

        public IEnumerable<EndpointOption> Options
        {
            get { return endpointOptions; }
        }

        public Endpoint() { }
        public Endpoint(IEnumerable<string> pieces)
        {
            foreach (var p in pieces)
                endpointPieces.Add(p);
        }
        public Endpoint(IEnumerable<string> pieces, IEnumerable<EndpointOption> options)
            : this(pieces)
        {
            foreach (var o in options)
                endpointOptions.Add(o);
        }

        public void Append(string piece)
        {
            if (piece == null)
                return;
            this.endpointPieces.Add(piece);
        }

        public void AddStaticPart(string value)
        {
            if (value == null)
                return;
            this.endpointPieces.Add(value);
        }
        public void AddPart(string value, string format)
        {
            if (value == null)
                return;
            value = value.Replace("/", "");
            value = value.TrimStart('.');
            this.endpointPieces.Add(format == null ? value : String.Format(format, value));
        }

        public void AddOption(EndpointOption option)
        {
            this.endpointOptions.Add(option);
        }
        public void AddOption(string key, string value)
        {
            if (value == null)
                return;
            this.AddOption(new EndpointOption(key, value));
        }
        public void AddOption(string key, string value, string format)
        {
            if (value == null)
                return;
            this.AddOption(new EndpointOption(key, format != null ? String.Format(format, value) : value));
        }
    }
}
